{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PyTorch / TF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],\n",
       "       [  0,   1,   2,   3,   4,   5,   6,   7,   8,   9],\n",
       "       [  0,   2,   4,   6,   8,  10,  12,  14,  16,  18],\n",
       "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],\n",
       "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],\n",
       "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],\n",
       "       [  0,  36,  72, 108, 144, 180, 216, 252, 288, 324],\n",
       "       [  0,  49,  98, 147, 196, 245, 294, 343, 392, 441],\n",
       "       [  0,  64, 128, 192, 256, 320, 384, 448, 512, 576],\n",
       "       [  0,  81, 162, 243, 324, 405, 486, 567, 648, 729]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as real_np\n",
    "\n",
    "x = real_np.arange(10)\n",
    "condlist = [x<3, x>5]\n",
    "choicelist = [x, x**2]\n",
    "selected = real_np.select(condlist, choicelist)\n",
    "real_np.multiply.outer(selected, x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.],\n",
       "       [  0.,   2.,   4.,   6.,   8.,  10.,  12.,  14.,  16.,  18.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,  36.,  72., 108., 144., 180., 216., 252., 288., 324.],\n",
       "       [  0.,  49.,  98., 147., 196., 245., 294., 343., 392., 441.],\n",
       "       [  0.,  64., 128., 192., 256., 320., 384., 448., 512., 576.],\n",
       "       [  0.,  81., 162., 243., 324., 405., 486., 567., 648., 729.]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def fn():\n",
    "    x = numpy.empty((10,10))\n",
    "\n",
    "    for i in range(10):\n",
    "        for j in range(10):\n",
    "            selected = i if i < 3 else (i ** 2 if i > 5 else 0)\n",
    "            x[i, j] = selected * j\n",
    "    return x\n",
    "fn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install torch torchvision"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],\n",
       "        [  0,   1,   2,   3,   4,   5,   6,   7,   8,   9],\n",
       "        [  0,   2,   4,   6,   8,  10,  12,  14,  16,  18],\n",
       "        [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],\n",
       "        [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],\n",
       "        [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],\n",
       "        [  0,  36,  72, 108, 144, 180, 216, 252, 288, 324],\n",
       "        [  0,  49,  98, 147, 196, 245, 294, 343, 392, 441],\n",
       "        [  0,  64, 128, 192, 256, 320, 384, 448, 512, 576],\n",
       "        [  0,  81, 162, 243, 324, 405, 486, 567, 648, 729]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "\n",
    "x = torch.arange(10)\n",
    "selected = torch.where(x < 3, x, torch.where(x > 5, x ** 2, torch.zeros_like(x)))\n",
    "torch.tensordot(selected, x, dims=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install tensorflow==2.0.0-alpha0 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=29, shape=(10, 10), dtype=int32, numpy=\n",
       "array([[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],\n",
       "       [  0,   1,   2,   3,   4,   5,   6,   7,   8,   9],\n",
       "       [  0,   2,   4,   6,   8,  10,  12,  14,  16,  18],\n",
       "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],\n",
       "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],\n",
       "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],\n",
       "       [  0,  36,  72, 108, 144, 180, 216, 252, 288, 324],\n",
       "       [  0,  49,  98, 147, 196, 245, 294, 343, 392, 441],\n",
       "       [  0,  64, 128, 192, 256, 320, 384, 448, 512, 576],\n",
       "       [  0,  81, 162, 243, 324, 405, 486, 567, 648, 729]], dtype=int32)>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "@tf.function\n",
    "def tf_fn():\n",
    "    x = tf.range(10)\n",
    "    selected = tf.where(x < 3, x, tf.where(x > 5, x ** 2, tf.zeros_like(x)))\n",
    "    return tf.tensordot(selected, x, axes=0)\n",
    "\n",
    "tf_fn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
